﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <script type="text/javascript">
        //不安全的构造函数 
        function Person(name, age) {
            this.name = name;
            this.age = age;
        }
        //name 和age被绑定到了window对象中
        Person("张三", 18);


        //-------------------------------------------------------------
        //安全的构造函数
        function Person(name, age) {
            if (this instanceof Person) {
                this.name = name;
                this.age = age;
            } else {
                return new Person(name, age);
            }
        }

        Person("张三", 18);
    </script>
</body>
</html>
